<?php

namespace app\communal\controller;

use app\Param;
use app\user\logic\Upload as IndexLogicUpload;
use app\model\SysSetting as ModelSysSetting;
use app\model\Area as ModelArea;

class Common extends Param
{
    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 地址列表 \n
     * URI : /area.list
     * @param :
     * name     | type   | null | description
     * ---------|--------|------|-------------
     * parentId | string | 选填  | 上级id
     *
     * @return :
     * name     | type   | description
     * ---------|--------|-------------
     *  name    | string | 名称
     *  areaId  | string | 地址id
     *
     */
    public function getAreaList()
    {
        $parent_id = (int)$this->_getContentParams('parentId');

        $list = ModelArea::getInstance()->toSelect(['parent_id' => $parent_id], 'name,id as areaId');

        _returnData(['areaList' => $list]);
    }

    /**
     * 上传图片 \n
     * URI : /public/img.upload
     * @param :
     *   name  |  type  | null | description
     * --------|--------|------|-------------
     *   type  | string | 必填  | 类型（headPortrait:头像，visitImg:走访情况图片,article:文章图片,logo:后台logo）
     *
     * @return
     *  name   |  type         | description
     * --------|---------------|-------------
     *  imgUrl | string(array) |  图片地址
     *
     */
    public function uploadImg()
    {
        $type = $this->_getContentParams('type');
        $res = '';
        switch ($type) {
            case 'headPortrait' :
                $res = IndexLogicUpload::getInstance()->saveFile('head_portrait'); //头像
                break;
            case 'idCardFront' :
                $res = IndexLogicUpload::getInstance()->saveFile('idCardFront'); //身份证正面
                break;
            case 'idCardBack' :
                $res = IndexLogicUpload::getInstance()->saveFile('idCardBack'); //身份证背面
                break;
            case 'workCard' :
                $res = IndexLogicUpload::getInstance()->saveFile('workCard'); //工牌
                break;
            case 'signForUser' :
                $res = IndexLogicUpload::getInstance()->saveFile('signForUser'); //确认收件签名
                break;
            default :
                _returnError(10008);
                break;
        }
        _returnData(['imgUrl' => $res]);
    }
    /*
     * 获取快递员经纬度
     * URI：'/public/getPosition'
     */
//    public function getPosition()
//    {
//        $lat = $this->_getContentParams('lat');
//        $lng = $this->_getContentParams('lng');
//        if ($lat && $lng){
//            $data['lat'] = $lat;
//            $data['lng'] = $lng;
//            return $data;
//        }else{
//            _returnError(15018);
//
//        }
//        return ['lat'=>'26.078','lng'=>'119.300'];
//    }

    /*
     * 返回提现手续费
     * URI:/public/procedure.fee
     */
    public function getProcedureFee()
    {
        $money = $this->_getContentParams('money');
        _toValidate('money',$money,11007);
//        $money = 100;
        $serverCharge = ModelSysSetting::getInstance()->getProcedureFee($money);
        _returnData(['serverCharge'=>$serverCharge]);

    }

}
